//将url的参数转为对象
var parseQueryString = function (url) {
    var reg_url = /^[^\?]+\?([\w\W]+)$/,
            reg_para = /([^&=]+)=([\w\W]*?)(&|$)/g, //g is very important
            arr_url = reg_url.exec(url),
            ret = {};
    if (arr_url && arr_url[1]) {
        var str_para = arr_url[1], result;
        while ((result = reg_para.exec(str_para)) != null) {
            ret[result[1]] = result[2];
        }
    }
    return ret;
};
//根据url的参数做动作
window.onload=function(){
	var wh=window.location.href.split("?");
	if(wh[1])
	{
		var winhref=parseQueryString(window.location.href);
		var dda = $(".thumbnail").length;
		for(var i=0;i<dda;i++)
		{
			var datapkg = $(".thumbnail .check-list input").eq(i).attr("data-pkg");
			var json=JSON.parse(datapkg);
			//console.log(json);
			if(json.pkgId == winhref.pkgId)
			{
				//console.log(json)
				var params={"pkgId":json.pkgId, "sign":json.sign}
				padMarketing.ajaxNormal(
					'/pad_marketing/main/type003getDecall.action',
					params,
					function(data){
						//console.log(data)
						$("#title").html(data.title+'      (投保年龄:'+data.subTitle+')');
						$('#subTitle').html('<span class="hide" data='+JSON.stringify(json)+'><span>')
						$("#benefit").html(data.benefit);
						$("#feature").html(data.feature);
						var imgsrc = '/portal/resources/images/product/packages/'+ data.pkgId  +'/CPTS.jpg';
						$('.show-pic').find('img').attr('src',imgsrc);
						var imgsrc1 = '/portal/resources/images/product/packages/'+ data.pkgId  +'/LYJJ.jpg';		
						$('.relative').find('img').attr('src',imgsrc1);
						var imgsrc2 = '/portal/resources/images/product/packages/'+ data.pkgId  +'/TBSL.jpg';		
						$('.show-eg').find('img').attr('src',imgsrc2);				
					},
					function(data){
						
					}
				);
				showDiv("open4");
				break;
			}			
		}		
	}
}

$(function(){
	
	var url = getQueryString('url');
	var come = getQueryString('come');
	var urlSecond=sessionStorage.allUrl;
	var comeSecond=sessionStorage.comePro
	if(url){
		//存储返回的url
		sessionStorage.setItem("allUrl", url);
		if(come){
			sessionStorage.setItem("comePro", come);
		}else{
			//隐藏建议书
			$('#advice').addClass('hide');
		}	
	}else{
		//不存在 隐藏返回键
		if(urlSecond){
			if(comeSecond){
				
			}else{
				$('#advice').addClass('hide');
			}
			
		}
		if(urlSecond){
			
		}else{
			$('#returnHref').hide();
		}
		
	} 
	nameInput();
	producecp();
	
	
	
	
	
	//出生日期
	var opt = {
        preset: 'date', //日期
        theme: 'android-ics light', //皮肤样式
        display: 'bottom', //显示方式 
        mode: 'scroller', //日期选择模式
        lang: 'zh',
        dateFormat: 'yy-mm-dd', // 日期格式
        setText: '确定', //确认按钮名称
        cancelText: '取消',//取消按钮名籍我
        dateOrder: 'yymmdd', //面板中日期排列格式
        dayText: '', monthText: '', yearText: '', //面板中年月日文字
        endYear:2020, //结束年份
        onSelect:function(valueText, inst){
        	//console.log(valueText);
        	$(this).next().val(valueText);
        	OnDateOfBirthChange(valueText,this);
        }
	};
	$('.input-group-addon').mobiscroll(opt);
/*	$('.input-group input:first').change(function(){
		$(this).parent().find('input:last').val(null);
		$('.input-group-addon').mobiscroll('setDate',new Date());
	});*/
});


function producecp(){
	
	
		var params = {};
		padMarketing.ajaxNormal('/pad_marketing/main/type004AllList.action',params,
				function(data){
//					//字符串转换成对象形式
			var viewAll = JSON.parse(data.message);
			//console.log(viewAll);
			//viewAll.L = viewAll.H[1];
			
			//健康H
			for(var i = 0; i < viewAll.H.length; i++) {
				var pag1 = $('<li class="thumbnail"><img src="/portal/resources/images/product/packages/'
						+ viewAll.H[i].pkgId 
						+'/ALL.jpg" alt="..."><div class="caption"><h3 class="font-16 p-overflow">'
						+ viewAll.H[i].title
						+ '</h3><p class="font-14 gray">' 
						+ viewAll.H[i].subTitle 
						+ '</p></div><div class="check-list default"><input data-pkg=\''
						+JSON.stringify(viewAll.H[i])
						+'\' type="checkbox"><label for="">&nbsp;</label></div></li>');
				pag1.data("data",viewAll.H[i]);
				$('#health').append(pag1);
			}
	  //退休R
			for(var i = 0; i < viewAll.R.length; i++) {
				var pag2 = $('<li class="thumbnail"><img src="/portal/resources/images/product/packages/'
						+ viewAll.R[i].pkgId  
						+'/ALL.jpg" alt="..."><div class="caption"><h3 class="font-16 p-overflow">'
						+ viewAll.R[i].title
						+ '</h3><p class="font-14 gray">' 
						+ viewAll.R[i].subTitle 
						+ '</p></div><div class="check-list default"><input data-pkg=\''
						+JSON.stringify(viewAll.R[i])
						+'\' type="checkbox"><label for="">&nbsp;</label></div></li>');
				pag2.data("data",viewAll.R[i]);
				$('#old').append(pag2);
			}
			//子女教育E
			for (var i= 0;i < viewAll.E.length; i++) {
				var pag3 = $('<li class="thumbnail"><img src="/portal/resources/images/product/packages/'
						+ viewAll.E[i].pkgId  
						+'/ALL.jpg" alt="..."><div class="caption"><h3 class="font-16 p-overflow">' 
						+ viewAll.E[i].title 
						+ '</h3><p class="font-14 gray">' 
						+ viewAll.E[i].subTitle 
						+ '</p></div><div class="check-list default"><input data-pkg=\''
						+JSON.stringify(viewAll.E[i])
						+'\' type="checkbox"><label for="">&nbsp;</label></div></li>');
				pag3.data("data",viewAll.E[i]);
				$('#edu').append(pag3);
			}
			 //財富F
		     for (var i= 0;i < viewAll.F.length; i++) {
		    	 //console.log(viewAll);
					var pag5 = $('<li  class="thumbnail"><img src="/portal/resources/images/product/packages/'
							+ viewAll.F[i].pkgId 
							+'/ALL.jpg" alt="..."><div class="caption"><h3 class="font-16 p-overflow">' 
							+ viewAll.F[i].title 
							+ '</h3><p class="font-14 gray">' 
							+ viewAll.F[i].subTitle 
							+ '</p></div><div class="check-list default"><input data-pkg=\''
							+JSON.stringify(viewAll.F[i])
							+'\' type="checkbox"><label for="">&nbsp;</label></div></li>');
					pag5.data("data",viewAll.F[i]);
					$('#plan').append(pag5);
		     }	
	      //人身L
		     if(viewAll.L){
		    	 for (var i= 0;i < viewAll.L.length; i++) {
						var pag4 = $('<li  class="thumbnail"><img src="/portal/resources/images/product/packages/'
								+ viewAll.L[i].pkgId  
								+'/ALL.jpg" alt="..."><div class="caption"><h3 class="font-16 p-overflow">'
								+ viewAll.L[i].title 
								+ '</h3><p class="font-14 gray">' 
								+ viewAll.L[i].subTitle 
								+ '</p></div><div class="check-list default"><input data-pkg=\''
								+JSON.stringify(viewAll.L[i])
								+'\' type="checkbox"><label for="">&nbsp;</label></div></li>');
						pag4.data("data",viewAll.L[i]);
						$('#life').append(pag4);
						
					}	    
		     }
	          	     
	     var returnUrl = sessionStorage.setItem('returnUrl', '/pad_marketing/main/type004redirectlistViewAll.action');  	
	     myScroll?myScroll.refresh():''; 
				}, function(data) {
					//alertMsg('失败',1);
				},false);		
	}
$(document).on('click','input',function(){
	//var slide = $(".check-list input:checkbox:checked").attr('data-pkg');	
	var slide = $(this).attr('data-pkg');	
	if(!slide)
		return;
	var obj = JSON.parse(slide);
	if(obj.allowSaling){		
		
	}else{
		$(".check-list  input").attr('info','1');
		alertMsg('此产品不可购买');
			$('#alertMsg_btn3').click(function(){
				$('input[info="1"]').prop("checked",false);
			})		
	}
})

//数量计算
$(document).on('click', '#health .check-list', function() {
	var num = $("#health .check-list  input:checkbox:checked").length;
	$('#health_num').text(num);
});
$(document).on('click', '#old .check-list', function() {
	var num = $("#old .check-list input:checkbox:checked").length;
	$('#old_num').text(num);
});
$(document).on('click', '#edu .check-list', function() {
	var num = $("#edu .check-list input:checkbox:checked").length;
	//console.log( $("#edu .check-list input:checkbox:checked"));
	$('#edu_num').text(num);
});
$(document).on('click', '#life .check-list', function() {
	var num = $("#life .check-list input:checkbox:checked").length;
	$('#life_num money_num').text(num);
	
});
$(document).on('click', '#plan .check-list', function() {
	var num = $("#plan .check-list input:checkbox:checked").length;
	$('#money_num').text(num);
	
});
	
//点击立即购买后输入被保险人信息

//点确定，生成出单
function appCreate(){
	var params = {};
	padMarketing.ajaxNormal('/pad_marketing/main/type004AllList.action',params,
			function(data){
				//console.log('success');
			},
			function(data){
				alertMsg('失败');
			}
	);
}
//tab 切换
$(document).on('click','.thumbnail',function(){
	$(".tabs-line a").eq(0).addClass("active").siblings().removeClass("active");
	$("#open4 .tabSwiper .swiper-wrapper .swiper-slide").eq(0).addClass("swiper-slide-active").siblings().removeClass("swiper-slide-active");
	$("#open4 .tabSwiper .swiper-wrapper").css({"transform":" translate3d(0px, 0px, 0px)","transition-duration": "0 s"});
	//将allowSaling 属性转移到title上面
	$("#title").data("data",$(this).eq(0).data("data"));
	
	var inputs = $(this).find('input');
	var div = eval('(' +$(inputs).attr('data-pkg') + ')');
	var params = {};
	params.pkgId = div.pkgId;
	params.sign = div.sign;

	if($("#leftTabBox")){
	 TouchSlide({ slideCell:"#leftTabBox" });
	};
	$('.show-pic').find('img').attr('src','');
	$('.relative').find('img').attr('src','');
	$('.show-eg').find('img').attr('src','');
	padMarketing.ajaxNormal(
			'/pad_marketing/main/type003getDecall.action',
			params,
			function(data){
				$("#title").html(data.title+'      (投保年龄:'+data.subTitle+')');
				$('#subTitle').html('<span class="hide" data='+JSON.stringify(div)+'><span>')
				$("#benefit").html(data.benefit);
				$("#feature").html(data.feature);
				var imgsrc = '/portal/resources/images/product/packages/'+ data.pkgId  +'/CPTS.jpg';
				$('.show-pic').find('img').attr('src',imgsrc);
				var imgsrc1 = '/portal/resources/images/product/packages/'+ data.pkgId  +'/LYJJ.jpg';		
				$('.relative').find('img').attr('src',imgsrc1);
				var imgsrc2 = '/portal/resources/images/product/packages/'+ data.pkgId  +'/TBSL.jpg';		
				$('.show-eg').find('img').attr('src',imgsrc2);
				var url=sessionStorage.allUrl;
				var come=sessionStorage.comePro;
				if(url){
					if(come){
						
					}else{
						$("#proButton").hide();
						$("#proButton").parent().removeClass('col-md-offset-3').addClass('col-md-offset-1');
					}
				}
				showDiv('open4');
			},
			function(data){
			}
		);
})
$(document).on('click','.check-list',function(event){
	 event.stopPropagation();
})



//这个是详情里面点击购买的方法
function checkGood(flag){
	//判断是否能购买
	var obj = $("#title").data("data");
	if(obj.allowSaling!="true"){
		alertMsg("该产品暂不能购买");
		return;
	}
	if(flag==1){
		$("#normalSure").removeAttr("index");
		$("#normalSure").attr("index",flag+1);
		showDiv('open1');
	}else if(flag==2){
		$("#normalSure").removeAttr("index");
		$("#normalSure").attr("index",flag+1);
		showDiv('open2')
	}
}
function complete(index){
	if($(".check-list  input:checkbox:checked").length==0){
		alertMsg("请您选择产品");
		return;
	}else{
		//点击建议书 或立即购买的标示位
		$("#normalSure").removeAttr("index");
		$("#normalSure").attr("index",index+1);
		showDiv('open'+index);
	}
}
$("#sureView").click(function(){		
	var dataOfBirth = $("#dataOfBirth").val();
	var  career     =$('input[id^=mobiscroll]').eq(0).val();
	if($('#open4').css('display') == 'block'){
		var slide = $("#subTitle").find('.hide').attr('data');
		var obj = JSON.parse(slide);
		var pkgs =[];
		pkgs.push(obj);	
	}
	
	if(dataOfBirth==""||career==""){
		alertMsg("请完整填写数据");
		return;
	}		
	closeDiv("open1");
	$('#popupLayerScreenLocker').css('display','none');
	saveCp3MinData(this,pkgs);
});



//建议书三要素创建
$('#surePro').click(function(){
var age = $("#inputAge").val();
var re = /^\d*$/;
if(!re.test(age)||age==""||age==null){
	alertMsg("请正确输入年龄");
	return;
}
	 var flag = alt(open2);	
	 if(flag){
		 closeDiv("open2");
		$('#popupLayerScreenLocker').css('display','none');
	 }else{
		 alertMsg('请输入完整信息');
		 return;
	 }
	 if($('#open4').css('display') == 'block'){
	 var slide = $("#subTitle").find('.hide').attr('data');
		var obj = JSON.parse(slide);
		//console.log(obj);
		var pkgs =[];
		pkgs.push(obj);
	 }
	 quoCreate(this,pkgs);
	
	
});
//点击返回 
function returnHref(){
	var url = sessionStorage.allUrl;
	sessionStorage.removeItem("allUrl");
	sessionStorage.removeItem("comePro")
	if(url){
		location.href=url;
		return;
	}
	location.href="/pad_marketing/index/index.action";
}
function returnHre(){	
	sessionStorage.removeItem("allUrl");
	sessionStorage.removeItem("comePro");
	location.href="/pad_marketing/index/index.action";
}